package com.durian.base.frame.dialog;

import android.graphics.Color;
import androidx.fragment.app.FragmentManager;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import com.durian.base.R;
import com.durian.base.utils.UIUtils;
import com.durian.ui.dialog.BaseNiceDialog;
import com.durian.ui.dialog.NiceDialog;
import com.durian.ui.dialog.ViewConvertListener;
import com.durian.ui.dialog.ViewHolder;
import com.durian.ui.textview.RoundButton;
import java.util.ArrayList;

/**
 * 说明：DialogBottomSelect
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-07-11 10:49
 * <p/>
 * 版本：version 1.0
 */
public class DialogBottomSelect {
    private ArrayList<MenuItem>     mMenuItems;
    private FragmentManager         mFragmentManager;
    private MenuItem                mCancelMenuItem;
    private OnMenuItemClickCallBack mCancelCallBack;

    public interface OnMenuItemClickCallBack{
        void onMenuItemClick(MenuItem item);
    }

    public static class MenuItem{

        private int id;
        private String text = "";
        private int textSize = 16;
        private int cancelTextColor = Color.parseColor("#5B92FD");
        private int textColor = Color.parseColor("#473F3F");
        private int pressedColor = Color.parseColor("#eeeeee");
        private int solidColor = Color.parseColor("#ffffff");
        private OnMenuItemClickCallBack mListener;

        private MenuItem(int id, String text) {
            this.id = id;
            this.text = text;
        }

        public static MenuItem create(String text){
            return new MenuItem(0,text);
        }

        public static MenuItem create(int id,String text){
            return new MenuItem(id,text);
        }

        public MenuItem setOnClickListener(OnMenuItemClickCallBack listener){
            this.mListener = listener;
            return this;
        }

        public OnMenuItemClickCallBack getOnClickListener(){
            return mListener;
        }

        public int getId(){
            return id;
        }

        public String getText(){
            return text;
        }

    }

    private DialogBottomSelect(FragmentManager manager){
        mMenuItems = new ArrayList<>();
        mFragmentManager = manager;
    }

    private MenuItem getCancelMenuItem(){
        return mCancelMenuItem == null ? MenuItem.create(-1, "取消").setOnClickListener(mCancelCallBack) : mCancelMenuItem;
    }

    public static DialogBottomSelect create(FragmentManager manager){
        return new DialogBottomSelect(manager);
    }

    public DialogBottomSelect insert(MenuItem item){
        if (item != null){
            mMenuItems.add(item);
        }
        return this;
    }

    public DialogBottomSelect insertList(ArrayList<MenuItem> items){
        if (items != null && !items.isEmpty()){
            mMenuItems.addAll(items);
        }
        return this;
    }

    public DialogBottomSelect setCancelCallBack(OnMenuItemClickCallBack cancelCallBack){
        this.mCancelCallBack = cancelCallBack;
        return this;
    }

    public DialogBottomSelect setCancelItem(MenuItem item){
        this.mCancelMenuItem = item;
        return this;
    }

    public DialogBottomSelect show(){
        if (mFragmentManager != null && !mMenuItems.isEmpty()){
            NiceDialog.init().setLayoutId(R.layout.base_dialog_bottom_selected)
                    .setConvertListener(new ViewConvertListener(){

                        private int lineColor = Color.parseColor("#E7E7E7");
                        private int corner = UIUtils.dip2px(12);
                        private int zero = 0;

                        private void addTopButton(final BaseNiceDialog dialog,LinearLayout llRoot,final MenuItem item){
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50));
                            RoundButton roundButton = new RoundButton(llRoot.getContext());
                            roundButton.setGravity(Gravity.CENTER);
                            roundButton.setRbTextColor(item.textColor);
                            roundButton.setCorner(corner,corner,zero,zero);
                            roundButton.setSolidColor(item.solidColor);
                            roundButton.setPressedColor(item.pressedColor);
                            roundButton.setText(item.text);
                            roundButton.setTextSize(item.textSize);
                            roundButton.setOnClickListener(v -> {
                                dialog.dismiss();
                                if (item.getOnClickListener() != null){
                                    item.getOnClickListener().onMenuItemClick(item);
                                }
                            });
                            llRoot.addView(roundButton,params);
                        }

                        private void addOneButton(final BaseNiceDialog dialog,LinearLayout llRoot,final MenuItem item){
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50));
                            RoundButton roundButton = new RoundButton(llRoot.getContext());
                            roundButton.setGravity(Gravity.CENTER);
                            roundButton.setRbTextColor(item.textColor);
                            roundButton.setCorner(corner,corner,corner,corner);
                            roundButton.setSolidColor(item.solidColor);
                            roundButton.setPressedColor(item.pressedColor);
                            roundButton.setText(item.text);
                            roundButton.setTextSize(item.textSize);
                            roundButton.setOnClickListener(v -> {
                                dialog.dismiss();
                                if (item.getOnClickListener() != null){
                                    item.getOnClickListener().onMenuItemClick(item);
                                }
                            });
                            llRoot.addView(roundButton,params);
                        }

                        private void addBottomButton(final BaseNiceDialog dialog,LinearLayout llRoot,final MenuItem item){
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50));
                            RoundButton roundButton = new RoundButton(llRoot.getContext());
                            roundButton.setGravity(Gravity.CENTER);
                            roundButton.setRbTextColor(item.textColor);
                            roundButton.setCorner(zero,zero,corner,corner);
                            roundButton.setSolidColor(item.solidColor);
                            roundButton.setPressedColor(item.pressedColor);
                            roundButton.setText(item.text);
                            roundButton.setTextSize(item.textSize);
                            roundButton.setOnClickListener(v -> {
                                dialog.dismiss();
                                if (item.getOnClickListener() != null){
                                    item.getOnClickListener().onMenuItemClick(item);
                                }
                            });
                            llRoot.addView(roundButton,params);
                        }

                        private void addCenterButton(final BaseNiceDialog dialog,LinearLayout llRoot,final MenuItem item){
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50));
                            RoundButton roundButton = new RoundButton(llRoot.getContext());
                            roundButton.setGravity(Gravity.CENTER);
                            roundButton.setRbTextColor(item.textColor);
                            roundButton.setCorner(zero,zero,zero,zero);
                            roundButton.setSolidColor(item.solidColor);
                            roundButton.setPressedColor(item.pressedColor);
                            roundButton.setText(item.text);
                            roundButton.setTextSize(item.textSize);
                            roundButton.setOnClickListener(v -> {
                                dialog.dismiss();
                                if (item.getOnClickListener() != null){
                                    item.getOnClickListener().onMenuItemClick(item);
                                }
                            });
                            llRoot.addView(roundButton,params);
                        }

                        private void addLine(LinearLayout llRoot){
                            View view = new View(llRoot.getContext());
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(0.1));
                            view.setBackgroundColor(lineColor);
                            llRoot.addView(view,params);
                        }

                        private void addCancelButton(final BaseNiceDialog dialog, LinearLayout llRoot){
                            final MenuItem item = getCancelMenuItem();
                            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(50));
                            params.setMargins(0, UIUtils.dip2px(10), 0, 0);
                            RoundButton roundButton = new RoundButton(llRoot.getContext());
                            roundButton.setGravity(Gravity.CENTER);
                            roundButton.setRbTextColor(item.cancelTextColor);
                            roundButton.setCorner(corner,corner,corner,corner);
                            roundButton.setSolidColor(item.solidColor);
                            roundButton.setPressedColor(item.pressedColor);
                            roundButton.setText(item.text);
                            roundButton.setTextSize(item.textSize);
                            roundButton.setOnClickListener(v -> {
                                dialog.dismiss();
                                if (item.getOnClickListener() != null){
                                    item.getOnClickListener().onMenuItemClick(item);
                                }
                            });
                            llRoot.addView(roundButton,params);
                        }

                        @Override
                        public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                            super.convertView(holder, dialog);
                            LinearLayout llRoot = holder.getView(R.id.ll_root);

                            if (mMenuItems.size() == 1){
                                addOneButton(dialog,llRoot,mMenuItems.get(0));
                            }else if (mMenuItems.size() == 2){
                                addTopButton(dialog,llRoot,mMenuItems.get(0));
                                addLine(llRoot);
                                addBottomButton(dialog,llRoot,mMenuItems.get(1));
                            }else if (mMenuItems.size() >= 3){
                                addTopButton(dialog,llRoot,mMenuItems.get(0));
                                addLine(llRoot);
                                for (int i = 1;i < mMenuItems.size() - 1;i++){
                                    addCenterButton(dialog,llRoot,mMenuItems.get(i));
                                    addLine(llRoot);
                                }
                                addBottomButton(dialog,llRoot,mMenuItems.get(mMenuItems.size() - 1));
                            }
                            addCancelButton(dialog,llRoot);
                        }
                    })
                    .setOutCancel(false)
                    .setShowBottom(true)
                    .setAnimStyle(R.style.base_dialog_bottom_animation)
                    .show(mFragmentManager);
        }
        return this;
    }
}
